﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CompanyBussiness;

namespace Website.admin.Control
{
    public partial class cMetadata : System.Web.UI.UserControl
    {
        NewsManagement newsMana = new NewsManagement();
        ProductManagement proMana = new ProductManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        CompanyBussiness.Category category;
        CompanyBussiness.TypeProduct typeProduct;
        CompanyBussiness.Language language;
        CompanyBussiness.New news;
        CompanyBussiness.Product product;
        CompanyBussiness.Metadata metadata;
        CheckValid cv = new CheckValid();
        bool AddOrUp = false;
        int STRnewsID=0;
        int STRproductID=0;
        int STRcategoryID = 0;
        int STRtypeProductID = 0;
        string error = "";
        string modulCode;

        protected void Page_Load(object sender, EventArgs e)
        {
            modulCode = Request.QueryString["Mod"];

            if (Request[CONST.QUERYSTRING_NEWS] != null && Request[CONST.QUERYSTRING_PRODUCT] == null && Request[CONST.QUERYSTRING_CATEGORY] == null && Request[CONST.QUERYSTRING_TYPE_PRODUCT] == null)
            {
                STRnewsID = int.Parse(Request[CONST.QUERYSTRING_NEWS]);
                news = newsMana.GetNewsByID(STRnewsID);
               
            }
            else if (Request[CONST.QUERYSTRING_NEWS] == null && Request[CONST.QUERYSTRING_PRODUCT] != null && Request[CONST.QUERYSTRING_CATEGORY] == null && Request[CONST.QUERYSTRING_TYPE_PRODUCT] == null)
            {
                STRproductID = int.Parse(Request[CONST.QUERYSTRING_PRODUCT]);
                product = proMana.GetProductByID(STRproductID);
                
            }
            else if (Request[CONST.QUERYSTRING_NEWS] == null && Request[CONST.QUERYSTRING_PRODUCT] == null && Request[CONST.QUERYSTRING_CATEGORY] != null && Request[CONST.QUERYSTRING_TYPE_PRODUCT] == null)
            {
                STRcategoryID = int.Parse(Request[CONST.QUERYSTRING_CATEGORY]);
                category = newsMana.GetCategoryByID(STRcategoryID, modulCode);
                
            }
            else if (Request[CONST.QUERYSTRING_NEWS] == null && Request[CONST.QUERYSTRING_PRODUCT] == null && Request[CONST.QUERYSTRING_CATEGORY] == null && Request[CONST.QUERYSTRING_TYPE_PRODUCT] != null)
            {
                STRtypeProductID = int.Parse(Request[CONST.QUERYSTRING_TYPE_PRODUCT]);
                typeProduct = proMana.GetTypeProductByID(STRtypeProductID);
                
            }
            this.LoadForm();
        }

        protected void LoadForm()
        {
            try
            {
                if(STRnewsID!=0)
                {
                    metadata = newsMana.GetMetadataByNewsID(STRnewsID);
                   // news = newsMana.GetNewsByID(STRnewsID);
                }
                else if(STRproductID!=0)
                {
                    metadata = newsMana.GetMetadataByProductID(STRproductID);
                    //product = proMana.GetProductByID(STRproductID);
                }
                else if (STRcategoryID != 0)
                {
                    metadata = newsMana.GetMetadataByCategoryID(STRcategoryID);
                   // category = newsMana.GetCategoryByID(STRcategoryID,modulCode);
                }
                else if (STRtypeProductID != 0)
                {
                    metadata = newsMana.GetMetadataByTypeProductID(STRtypeProductID);
                   // typeProduct = proMana.GetTypeProductByID(STRtypeProductID);
                }
                txtTitle.Text = metadata.Title;
                txtKeyword.Text = metadata.MetaKeyword;
                txtDescription.Text = metadata.MetaDesciption;
                
            }
            catch (Exception)
            { 
                
            }
        }

        protected void GetData()
        {
            if (txtTitle.Text.Trim() != "")
            {
                metadata.Title = txtTitle.Text;
            }
            else
            {
                 if(STRnewsID!=0)
                {
                    news = newsMana.GetNewsByID(STRnewsID);
                    metadata.Title = news.NewsSubject.ToString();
                }
                else if(STRproductID!=0)
                {
                    product = proMana.GetProductByID(STRproductID);
                    metadata.Title = product.ProductName.ToString();
                }
                else if (STRcategoryID != 0)
                {
                    category = newsMana.GetCategoryByID(STRcategoryID, modulCode);
                    metadata.Title = category.CategoryName.ToString();
                }
                else if (STRtypeProductID != 0)
                {
                    typeProduct = proMana.GetTypeProductByID(STRtypeProductID);
                    metadata.Title = typeProduct.TypeProductName.ToString();
                }
            }

            metadata.MetaKeyword = txtKeyword.Text.ToString();
            metadata.MetaDesciption = txtDescription.Text.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                    #region Update

                    GetData();

                    newsMana.UpdateMetadata(metadata);

                    #endregion

                    //Quay ve danh muc vua tao 

                    if (STRnewsID != 0)
                    {
                        Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CreateNews&" + CONST.QUERYSTRING_NEWS + "=" + STRnewsID + "&Mod=" + modulCode);
                    }
                    else if (STRproductID != 0)
                    {
                        Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CreateProduct&" + CONST.SESSION_PRODUCT_ID + "=" + STRproductID + "&Mod="+modulCode);
                    }
                    else if (STRcategoryID != 0)
                    {
                        Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=CreateCategory&" + CONST.QUERYSTRING_CATEGORY + "=" + STRcategoryID +"&Mod=" + modulCode);
                    }
                    else if (STRtypeProductID != 0)
                    {
                        Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=ParentTypeProductDetail&" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + STRtypeProductID + "&Mod=" + modulCode);
                    }
                    

                //this.LoadForm();
            }
            catch (Exception ex)
            {
            }   
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.LoadForm();
        }
    }
}